//
//  OrderVC.m
//  GasBusiness
//
//  Created by zfc on 2021/8/22.
//

#import "OrderVC.h"

@interface OrderVC () <UIScrollViewDelegate, SPPageMenuDelegate>
@property (nonatomic, strong) SPPageMenu *sp;
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation OrderVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"我的订单";
    [self createSp];
    [self addViewControllsToScrollView];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"threeBtnClick" object:nil];
}


- (void) createSp {
    self.sp = [SPPageMenu pageMenuWithFrame:CGRectZero trackerStyle:SPPageMenuTrackerStyleLine1];
    self.sp.permutationWay = SPPageMenuPermutationWayNotScrollAdaptContent;

    self.sp.selectedItemTitleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
    self.sp.unSelectedItemTitleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.sp.dividingLine.hidden = YES;
    self.sp.delegate = self;
    //    self.sp.spacing = 20;
        self.sp.lineOffset = -14;

    self.sp.selectedItemTitleColor = PriceColor;
    self.sp.tracker.backgroundColor = PriceColor;
    [self.sp setTrackerHeight:2 cornerRadius:0];
    self.sp.unSelectedItemTitleColor = Color333;
    [self.view addSubview:self.sp];

    NSArray *arr = @[@"订单池", @"待接单", @"已接单", @"已完成", @"已关闭"];
    [self.sp setItems:arr selectedItemIndex:0];

    [self.sp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.leading.equalTo(self.view).offset(5);
        make.trailing.equalTo(self.view).offset(-5);
        make.height.mas_equalTo(50);
    }];

    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    self.scrollView.backgroundColor = WhiteColor;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth*6, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.backgroundColor = WhiteColor;

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.sp.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
}

-(void)addViewControllsToScrollView {
    for (int i = 0; i < 5; i ++) {
        OrderListVC *vc = [[OrderListVC alloc] init];
        vc.view.backgroundColor = ColorF5;
        switch (i) {
            case 0: //全部
                break;
            case 1: //待付款
                vc.synthesize_status = @"0";
                break;
            case 2: //待接单
                vc.synthesize_status = @"105";
                break;
            case 3: //已接单
                vc.synthesize_status = @"106";
                break;
            case 4: //已完成
                vc.synthesize_status = @"4";
                break;
            case 5: //已关闭
                vc.synthesize_status = @"-4";
                break;
            default:
                break;
        }

        vc.view.frame = CGRectMake(kScreenW*i, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];

    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"threeBtnClick" object:nil];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int index = (scrollView.contentOffset.x/kScreenWidth);
    if (self.sp.selectedItemIndex == index) {
        return;
    }
    self.sp.selectedItemIndex = index;
}

#pragma mark - SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth*index, 0) animated:YES];

    if (self.childViewControllers.count > 0) {
        OrderListVC *vc = self.childViewControllers[index];
        [vc fetchData];
    }
}



@end
